package wowmobile;

import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Player extends Thread {

    Socket channel;
    Communication c;
    Coordinate place;
    Area area;
    Server server;
    private PlayerStats stats;
    private Party party;
    boolean fight;
    private Quest q;
    
    //dopisać przekazywanie party przez leadera
    /*
     * trzeba regulować hp i manę
     * wczytać hp, manę, defence, power, speed, level resetować po walce
     */
    Player(Socket channel, Server server, String login)
    {
        this.stats = Database.PlayerfromDatabase(login);
        this.server = server;
        this.channel = channel;
        c = new Communication(channel);
        place = Database.CoordinatefromDatabase(login);
        place.setTurn(1);
        area = Database.AreaFromDatabase(login);
        q=new Quest();
        try {
            c.sendArea(area);
        } catch (IOException e) {
            System.err.println("Problem przy wysylanie Area:" + e);
        }

        /*
         * calibrateStats();
         * trzeba wpisać tutaj i przed każdą walką i po każdej walce, oraz przy każdej zmianie itema
         * za każdym razem regeneruje się hp i mana do maxa
         */
    }

    public Party getParty() {
        return party;
    }

    public PlayerStats getStats() {
        return stats;
    }

    public void setParty(Party party) {
        this.party = party;
    }

    @Override
    public void run() {
        int lubudubu;
        Action a=new Action();
        String answer;
        while (true) {
            try {
                answer="FF";
                if(!fight)
                c.sendPlayers(server.getPlayers(area, this));
                //System.out.println(area);
                if(!fight)
                a = c.getAction();
                if(!fight)
                updateCoordinate(a);
                //System.out.println(a.getMove());
                if(!fight)
                if(a.getMove()!=0 && a.getMove()<=4)
                    answer=prepareAnswer();
                 if(!fight)
                 if(answer.startsWith("T"))
                     new Fight(c, area.getItemAt(place.getX(), place.getY()),this, answer).fight();
                 else
                     c.sendAnswer(answer);
                while(fight)
                    lubudubu=0;
            } catch (Exception e) {
                System.out.println("One less... " + stats.getNick() + " left the game cause:"+e);
                Database.PlayertoDatabase(stats,place,area);
                server.deletePlayer(this);
                break;
            }
        }
    }

    private String prepareAnswer() {
        String answer = "";
        if (area.getItemAt(place.getX(), place.getY()).isAtacked()) {
            answer += "T";
        } else {
            answer += "F";
        }
        if (area.getItemAt(place.getX(), place.getY()).isPassage()) {
            answer += "T";
        } else {
            answer += "F";
        }
        return answer;
    }

    private void updateCoordinate(Action a) {
        int x = place.getX();
        int y = place.getY();
        switch (a.getMove()) {
            case Action.UP:
                if (area.getItemAt(x, y - 1).isWalkAble()) {
                    place.setTurn(2);
                    place.setY(y - 1);
                }
                break;
            case Action.DOWN:
                if (area.getItemAt(x, y + 1).isWalkAble()) {
                    place.setTurn(1);
                    place.setY(y + 1);
                }
                break;
            case Action.LEFT:
                if (area.getItemAt(x - 1, y).isWalkAble()) {
                    place.setX(x - 1);
                    place.setTurn(4);
                }
                break;
            case Action.RIGHT:
                if (area.getItemAt(x + 1, y).isWalkAble()) {
                    place.setX(x + 1);
                    place.setTurn(3);
                }
                break;
            case 5:
                if(a.getMenuOption()==1)
                {
                    try
                    {
                        c.sendAnswer(Parser.parsePlayer(this));
                    }
                    catch (IOException ex)
                    {
                        Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
                        return;
                    }
                }
                else if(a.getMenuOption()==2)
                {
                    String tmp="";
                    try {
                        tmp = c.getFightAction();
                    }
                    catch (IOException ex) {
                        Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    Player p;
                    try {
                        p = server.getPlayerByName(tmp);
                    } catch (Exception ex) {
                        Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
                        return;
                    }
                    party=new Party(this,p);
                    p.setParty(party);
                }
                else if(a.getMenuOption()==3)
                {
                    try
                    {

                        c.sendAnswer(q.toString());
                    }
                    catch (IOException ex)
                    {
                        Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
                        return;
                    }
                }
                break;
            case 6:
                if(place.getTurn()==1)
                {
                    if(area.getItemAt(place.getX(), place.getY()+1).isQuestGiver())
                        q=area.getItemAt(place.getX(), place.getY()+1).getQuest();
                }
                else if(place.getTurn()==2)
                {
                    if(area.getItemAt(place.getX(), place.getY()-1).isQuestGiver())
                        q=area.getItemAt(place.getX(), place.getY()-1).getQuest();
                }
                else if(place.getTurn()==3)
                {
                    if(area.getItemAt(place.getX()+1, place.getY()).isQuestGiver())
                        q=area.getItemAt(place.getX()+1, place.getY()).getQuest();
                }
                else
                {
                    if(area.getItemAt(place.getX()-1, place.getY()).isQuestGiver())
                        q=area.getItemAt(place.getX()-1, place.getY()).getQuest();
                }
                break;
        }
        try {
            c.sendAnswer(place.getX() + " " + place.getY());
        } catch (IOException ex) {
            Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    synchronized void setFight(boolean b) {
        fight=b;
    }

    public boolean isFight() {
        return fight;
    }

    synchronized public Communication getC() {
        return c;
    }

    public void setC(Communication c) {
        this.c = c;
    }

    public Quest getQ() {
        return q;
    }

    public void setQ(Quest q) {
        this.q = q;
    }



}
